package DataStructure.arrayAndList.list.listReverse;

import DataStructure.arrayAndList.list.ListReverse;
import DataStructure.arrayAndList.list.Nodelj;

/**
 * @author liujun
 * @version 1.0
 * @date 2021/12/7
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 利用指针关系 反转链表
 */
public class ListReverseByMultiPoint extends ListReverse {

    @Override
    public Nodelj reverse(Nodelj root) {
        if (root == null) {
            return null;
        }
        Nodelj newNode = new Nodelj();
        Nodelj cur = root;
        Nodelj pre = root;
        while (cur != null) {
            pre = cur;
            cur = cur.next;
            pre.next = newNode.next;
            newNode.next = pre;
        }
        return newNode.next;
    }

    public Nodelj reverse1(Nodelj root) {
        if (root == null) {
            return null;
        }
        Nodelj pre = root;
        Nodelj cur = root;
        Nodelj newRoot = null;
        while (cur != null) {
            //当前节点前进
            pre = cur;
            cur = cur.next;
            // 新节点更新
            pre.next = newRoot;
            newRoot = pre;
        }
        return newRoot;
    }
}
